/*
 * 系统项目名称：综合决策分析系统-version2.0
 * 文件名：ParamValidateServiceImpl.java
 * 描述：
 * 版权：Copyright 2015-2020 义乌市跨境电商供应链管理有限公司-版权所有
 * 修改人：yangjx
 * 修改时间：2016-3-10 下午1:23:39
 * 版本号：V1.1
 */
package com.ywt.cda.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.ywt.cda.action.model.BaseParamModel;
import com.ywt.cda.service.ParamValidateService;

/**
 * （一句话功能简述） <br>
 * （功能详细描述）
 * 
 * @author yangjx
 * @version V1.0
 * @see [相关类/方法]
 * @since 2016-3-10 下午1:23:39
 */
@Service
public class ParamValidateServiceImpl implements ParamValidateService {

	private final Logger logger = Logger
			.getLogger(ParamValidateServiceImpl.class);

	private static String YYYY_MM = "^[1-9]\\d{3}[-](([0][1-9])|([1][0-2]))$";

	private static String YYYY_M = "^[1-9]\\d{3}[-][1-9]$";

	private static String YYYY_M_D = "^[1-9]\\d{3}[-][1-9][-][1-9]$";

	private static String YYYY_MM_D = "^[1-9]\\d{3}[-](([0][1-9])|([1][0-2]))[-][1-9]$";

	private static String YYYY_M_DD = "^[1-9]\\d{3}[-][1-9][-](([0][1-9])|([1-2][0-9])|([3][0-1]))$";

	private static String YYYY_MM_DD = "^[1-9]\\d{3}[-](([0][1-9])|([1][0-2]))[-](([0][1-9])|([1-2][0-9])|([3][0-1]))$";

	@Override
	public boolean validate(BaseParamModel baseParamModel) {
		boolean flag = true;
		if (null == baseParamModel) {
			logger.info("model is null");
			flag = false;
			return flag;
		}
		if (StringUtils.isBlank(baseParamModel.getEndTime())) {
			logger.info("endtime is blank");
			flag = false;
			return flag;
		}
		if (StringUtils.isBlank(baseParamModel.getStartTime())) {
			logger.info("starttime is blank");
			flag = false;
			return flag;
		}
		String startTime = baseParamModel.getStartTime();
		String endTime = baseParamModel.getEndTime();

		String starttimeFormat = "";
		String endTimeFormat = "";
		boolean isSmatch = startTime.matches(YYYY_M);
		boolean isEmatch = endTime.matches(YYYY_M);

		if (isSmatch) {
			starttimeFormat = "yyyy-MM";
		}
		if (isEmatch) {
			endTimeFormat = "yyyy-MM";
		}
		if (!isSmatch) {
			isSmatch = isSmatch || startTime.matches(YYYY_M_D);
			if (isSmatch) {
				starttimeFormat = "yyyy-MM-dd";
			}
		}
		if (!isEmatch) {
			isEmatch = isEmatch || endTime.matches(YYYY_M_D);
			if (isEmatch) {
				endTimeFormat = "yyyy-MM-dd";
			}
		}

		
		
		if (!isSmatch) {
			isSmatch = isSmatch || startTime.matches(YYYY_MM);
			if (isSmatch) {
				starttimeFormat = "yyyy-MM";
			}
		}
		if (!isEmatch) {
			isEmatch = isEmatch || endTime.matches(YYYY_MM);
			if (isEmatch) {
				endTimeFormat = "yyyy-MM";
			}
		}
		
		
		if (!isSmatch) {
			isSmatch = isSmatch || startTime.matches(YYYY_MM_D);
			if (isSmatch) {
				starttimeFormat = "yyyy-MM-dd";
			}
		}
		if (!isEmatch) {
			isEmatch = isEmatch || endTime.matches(YYYY_MM_D);
			if (isEmatch) {
				endTimeFormat = "yyyy-MM-dd";
			}
		}
		
		
		if (!isSmatch) {
			isSmatch = isSmatch || startTime.matches(YYYY_MM_DD);
			if (isSmatch) {
				starttimeFormat = "yyyy-MM-dd";
			}
		}
		if (!isEmatch) {
			isEmatch = isEmatch || endTime.matches(YYYY_MM_DD);
			if (isEmatch) {
				endTimeFormat = "yyyy-MM-dd";
			}
		}
		
		
		if (!isSmatch) {
			isSmatch = isSmatch || startTime.matches(YYYY_M_DD);
			if (isSmatch) {
				starttimeFormat = "yyyy-MM-dd";
			}
		}
		if (!isEmatch) {
			isEmatch = isEmatch || endTime.matches(YYYY_M_DD);
			if (isEmatch) {
				endTimeFormat = "yyyy-MM-dd";
			}
		}
		
		
		if (!isSmatch) {
			logger.info("starttime is not formated");
			flag = false;
			return flag;
		}
		if (!isEmatch) {
			logger.info("endtime is not formated");
			flag = false;
			return flag;
		}
		if(!starttimeFormat.equals(endTimeFormat)){
			logger.info("endtime and starttime is not same formated ");
			flag = false;
			return flag;
		}
		SimpleDateFormat startDateFormat = new SimpleDateFormat(starttimeFormat);
		SimpleDateFormat endDateFormat = new SimpleDateFormat(endTimeFormat);
		try {
			Date startDate = startDateFormat.parse(startTime);
			Date endDate = endDateFormat.parse(endTime);
			if (startDate.after(endDate)) {
				logger.info("starttime after endtime");
				flag = false;
				return flag;
			}
			baseParamModel.setStartTime_F(startDateFormat.format(startDate));
			baseParamModel.setEndTime_F(endDateFormat.format(endDate));
			baseParamModel.setStartTimePattern(starttimeFormat);
			baseParamModel.setEndTimePattern(endTimeFormat);
		}
		catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("date format error");
			flag = false;
			return flag;
		}

		return flag;
	}

	public static void main(String[] args) {
		System.out.println("2016-2".matches(YYYY_M));
		System.out.println("1000-10".matches(YYYY_MM));
		System.out.println("1000-11-2".matches(YYYY_MM_D));
		System.out.println("1000-2-2".matches(YYYY_M_D));
		System.out.println("1000-1-22".matches(YYYY_M_DD));
		System.out.println("1000-01-30".matches(YYYY_MM_DD));
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM");
		try {
			Date d = fmt.parse("2000-1");
			System.out.println(d);
		}
		catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
